<?php

namespace app\admin\controller\auth;

use think\Controller;
use think\Request;
use app\admin\controller\Base;

class Rule extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if(request()->isPost()){
            $sorts=input('post.');
            foreach ($sorts as $k => $v) {
                model('AuthRule')->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('index'));
        }
        //$ruleList = Db::name('auth_rule')->field('id,name,title,status,sort,level')->select();
        $ruleList =model('AuthRule')->authRuleTree();
        $this->assign('ruleList', $ruleList);
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $ruleList =model('AuthRule')->authRuleTree();
        $this->assign('ruleTitle', $ruleList);
        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if (!request()->isPost()) {
            $this->error('非法请求！');
        }
        $data = $request->post();

        $validate=validate('AuthRule');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        if ($data['pid'] != 0) {
            $level = db('auth_rule')->where('id', $data['pid'])->field('level')->find();
            $data['level'] = $level['level'] + 1;
        }
        $res = db('auth_rule')->insert($data);
        if ($res) {
            $this->success('添加成功', url('index'));
        } else {
            $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update()
    {
        if(request()->isPost()){
            $data=input('post.');
            $save=db('authRule')->update($data);
            if($save!==false){
                $this->success('修改规则成功！','index');
            }else{
                $this->error('修改规则失败！');
            }
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = db('auth_rule')->delete($id);
        if($res == 1){
            $this->success('删除成功', url('index'));
        }else{
            $this->error('删除失败');
        }
    }

    public function listAll()
    {
        return view();
    }

    public function changeStatus($id,$status){
        $res = db('authRule')->update(['status' => $status,'id' => $id]);
        if($res == 1){
            $this->success('状态修改成功', url('index'));
        }else{
            $this->error('状态修改失败');
        }
    }
}
